<template>
<!-- 轮播图 -->
  <div class="hello">
    <div class="carousel-wrap" id="carousel">
        <!--  轮播图列表 -->
        <transition-group tag="ul" class='slide_ul' name="list">
          <!-- @mouseenter="stop" 鼠标在上面停止     @mouseleave="go"  鼠标离开就继续循环 -->
          <li class="slide_li" v-for="(list,index) in slideList" :key="list.desc" v-show="index===currentIndex" @mouseenter="stop"
            @mouseleave="go">
            <a :href="list.clickUrl">
              <img class="slide_img" :src="list.image" :alt="list.desc">
            </a>
          </li>
        </transition-group>
        <!-- 轮播图位置指示 -->
        <div class="carousel-items">
          <span class="items_span" v-for="(item,index) in slideList.length" :class="{'active':index===currentIndex}"
            @mouseover="change(index)" :key="index.desc"></span>
        </div>
      </div>
  </div>
  
</template>

<script>
import slideListImg1 from '../assets/lun4.jpg'
import slideListImg2 from '../assets/lun3.jpg'
import slideListImg3 from '../assets/lun2.jpg'
  export default {
    data() {
      return {
        slideList: [{
            "clickUrl": "#",
            "desc": "x1",
            "image": slideListImg1
          },
          {
            "clickUrl": "#",
            "desc": "x2",
            "image": slideListImg2
          },
          {
            "clickUrl": "#",
            "desc": "x3",
            "image": slideListImg3
          }
        ],
        currentIndex: 0,
        timer: ''
      }
    },
    methods: {
      go() {
        this.timer = setInterval(() => {
          this.autoPlay()
        }, 4000)
      },
      stop() {
        clearInterval(this.timer)
        this.timer = null
      },
      change(index) {
        this.currentIndex = index
      },
      autoPlay() {
        this.currentIndex++
        if (this.currentIndex > this.slideList.length - 1) {
          this.currentIndex = 0
        }
      }
    },
    created() {
      //在DOM加载完成后，下个tick中开始轮播
      this.$nextTick(() => {
        this.timer = setInterval(() => {
          this.autoPlay()
        }, 4000)
      })
    },
  }
</script>
<style scoped>
  .hello {
    margin: 0 auto;
    max-width: 2100px;
  }
  * {
    margin: 0;
    padding: 0;
  }
  .carousel-wrap {
    position: relative;
    height: 600px;
    width: 100%;
    overflow: hidden;
    /* // 删除 */
    background-color: #fff;
  }

  .slide_ul {
    width: 100%;
    height: 100%;
    text-align: center;
  }

  .slide_li {
    position: absolute;
    width: 100%;
    height: 100%;
  }

  .slide_img {
    width: 100%;
    height: 100%;
    max-width: 2000px;
  }

  .carousel-items {
    position: absolute;
    z-index: 10;
    top: 590px;
    width: 100%;
    margin: 0 auto;
    text-align: center;
    font-size: 0;
  }

  .items_span {
    display: inline-block;
    height: 30px;
    width: 50px;
    margin: 0 10px;
    background-color: #b2b2b2;
    cursor: pointer;
  }

  /* 窗口小于1800 */
  @media only screen and (max-width: 1800px) {
    .carousel-items {
      top: 580px;
    }

    .items_span {
      height: 18px;
      width: 80px;
      background-color:slategray;
      margin: 0 10px;
    }
  }

  /* 窗口小于1024 */
  @media only screen and (max-width: 1024px) {
    .hello {
      margin: 0 auto;
      max-width: 1024px;
    }

    .carousel-wrap {
      height: 400px;
    }

    .slide_img {
      max-width: 1024px;
    }

    .carousel-items {
      top: 380px;
    }

    .items_span {
      height: 12px;
      width: 60px;
      background-color:slategray;
      margin: 0 3px;
    }
  }

  /* 窗口小于768 */
  @media only screen and (max-width: 768px) {
    .hello {
      margin: 0 auto;
      max-width: 768px;
    }

    .carousel-wrap {
      height: 300px;
    }

    .slide_img {
      max-width: 768px;
    }

    .carousel-items {
      top: 280px;
    }

    .items_span {
      height: 15px;
      width: 80px;
      background-color:slategray;
      margin: 0 8px;
    }
  }

  /* 窗口小于768 */
  @media only screen and (max-width: 480px) {
    .hello {
      margin: 0 auto;
      max-width: 480px;
    }

    .carousel-wrap {
      height: 200px;
    }

    .slide_img {
      max-width: 480px;
    }

    .carousel-items {
      top: 190px;
    }

    .items_span {
      height: 12px;
      width: 40px;
      background-color:slategray;
      margin: 0 3px;
    }
  }
</style>
